package aimis.controller;

import aimis.domain.PlantingRecords;
import aimis.domain.Region;
import aimis.domain.TobaccoCrops;
import aimis.dto.Res;
import aimis.dto.form.AreaForm;
import aimis.dto.view.FyView;
import aimis.repo.AreaMapper;
import aimis.service.AreaService;
import aimis.service.impl.AreaServiceImpl;
import aimis.util.FyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Area")
public class AreaController {
    @Autowired
    AreaServiceImpl areaService;
    @Autowired
    AreaMapper areaMapper;

    @GetMapping("/hello")
    public Res<?> hello(){
        return Res.success();
    }
    //region_id 查询
    @PostMapping("/RegIdFindArea")
    public Res<?> RegIdFindArea(@RequestParam(value = "Rid") int RegID){
        return Res.success(areaService.RegIdFind(RegID),"查询成功");
    }
    //sowing_date 查询
    @PostMapping("/SowingDateFindArea")
    public Res<?> SowingDateFindArea(@RequestParam(value = "Swd") Date SowingDate) {
        return Res.success(areaService.SowingDateFind(SowingDate),"查询成功");
    }

    @PostMapping("/updatePlantingRecords")
    public Res<?> updatePlantingRecords(AreaForm areaForm){
        System.out.println(areaForm);
        PlantingRecords plan =  areaService.IdFindPlanting(areaForm.getId());
        areaService.updateAres(areaForm.getName1(), areaForm.getPlantedArea(), "1", plan.getRegionid());
        areaService.updateTobacco(areaForm.getName2(),areaForm.getTime(),areaForm.getUrl(),plan.getCropid());
        return Res.success();
    }


    @PostMapping("/InsertPlantingRecords")
    public Res<?> InsertPlantingRecords(AreaForm areaForm){
        System.out.println(areaForm);
        areaService.InsertTobacco(areaForm.getName2(), areaForm.getTime(), areaForm.getUrl());
        areaService.InsertAres(areaForm.getName1(), areaForm.getPlantedArea(), "1");
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Date formattedDate = Date.valueOf(today.format(formatter));
        areaService.InsertPlanting(areaMapper.NewRid(), areaMapper.NewTobaccoId(), Float.parseFloat(areaForm.getPlantedArea()),formattedDate,formattedDate,areaForm.getNumber());
        return Res.success();
    }

    @PostMapping("/Fy/{pageSize}/{pageNum}")
    public Res<?> Fy(@RequestBody List<Map<?,?>> list , @PathVariable("pageSize") int pageSize, @PathVariable("pageNum") int pageNum){
        List<Map<?, ?>> list1 = FyUtils.getlist(list,pageNum,pageSize,list.size());
        FyView fyView = new FyView(list.size() , list1);
        return Res.success(fyView,"成功");
    }


    @PostMapping("/getAllSearch")
    public Res<?> getAllSearch(@RequestParam(value = "search") String keyWord){
        return Res.success(areaService.getAllSearch(keyWord),"查询成功");
    }

    @DeleteMapping("/{id}")
    public Res deletePlantingRecord(@PathVariable Long id) {
        areaService.deletePlantingRecordById(id);
        return Res.success();
    }


}
